<template>
    <van-button v-if="isFollowed"
             :loading="loading"
             class="follow-btn"
              round
              size="small"
              @click="onfollow"
            >已关注</van-button>
  <van-button v-else
             :loading="loading"
              type="info"
              color="#3296fa"
              round
              size="small"
              icon="plus"
              class="follow-btn"
              @click="onfollow"
            >关注</van-button>

</template>

<script>
import { addFollow, deleteFollow } from '@/api/user.js'
export default {
  name: 'FollowUser',
  components: {},
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onfollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
          await deleteFollow(this.userId)
        } else {
          await addFollow(this.userId)
        }
        this.$emit('update_is_followed', !this.isFollowed)
      } catch (err) {
        let message = '操作失败，请重试！'
        if (err.response && err.response.status === 400) {
          message = '你不能关注你自己！'
        }
        if (err.response && err.response.status === 401) {
          message = '用户未登录，请先登录！'
        }
        this.$toast(message)
        window.console.log(err)
      }
      this.loading = false
    }

  }
}
</script>

 <style scoped lang="less">

</style>
